Skip to content

comdb2_atomic.h: Make Solaris atomics match Linux and cheapen loads - #6508

Open
akshatsikarwar wants to merge 1 commit into
bloomberg:mainfrom
akshatsikarwar:atomic
Open

akshatsikarwar wants to merge 1 commit into
bloomberg:mainfrom
akshatsikarwar:atomic

Conversation

@akshatsikarwar

@akshatsikarwar akshatsikarwar commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

See commit message for properly formatted changelog

mponomar
mponomar previously approved these changes Nov 20, 2026

@roborivers roborivers left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cbuild submission: Error ⚠.
RoboMark testing: submission_error.
Regression testing: Success ✓.

@roborivers roborivers left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cbuild submission: Error ⚠.
RoboMark testing: submission_error.
Regression testing: Success ✓.

The first 10 failing tests are:
cdb2api_unit [AddressSanitizer error]

@akshatsikarwar
akshatsikarwar force-pushed the atomic branch 2 times, most recently from 9962366 to 5d6a58d Compare November 24, 2026 14:45

@roborivers roborivers left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cbuild submission: Error ⚠.
RoboMark testing: submission_error.
Regression testing: 0/67 tests failed ⚠.

@roborivers roborivers left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cbuild submission: Success ✓.
RoboMark testing: pending.
Regression testing: 1/802 tests failed; harness exit 0 ⚠.

The first 10 failing tests are:
tsa

@roborivers roborivers left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cbuild submission: Success ✓.
RoboMark testing: success.
Regression testing: 1/802 tests failed; harness exit 0 ⚠.

The first 10 failing tests are:
tsa

mponomar
mponomar previously approved these changes Nov 25, 2026
The Solaris branch differed from the Linux one in ways that compiled
cleanly but behaved differently:

- CAS32/CAS64 did not write the observed value back into oldv on
  failure, as __atomic_compare_exchange_n does. The usual Linux retry
  loop, while (!CAS32(x, old, old + 1));, spins forever on Solaris.
  No current caller relies on it; all reload before retrying.
- CAS cast &mem to uint32_t * / uint64_t *, so CAS32 on a 64-bit word
  compiled. gcc builtins accept any width, so the same code works on
  Linux but on big-endian Solaris it compares and swaps only the high
  half.
- Results were always uint32_t/uint64_t, so ATOMIC_ADD32(int_x, -1) < 0
  was true on Linux and false on Solaris.
- ATOMIC_LOAD32/64 was atomic_add_*_nv(&mem, 0): a read-modify-write
  that takes the cache line exclusive. On a SPARC T5 it cost 36ns
  against 0.3ns for a plain load, and 566ns per load with 16 threads
  reading one word.
- ATOMIC_STORE32_REL used membar_producer (StoreStore), which is not a
  release barrier on its own.

Solaris now follows Linux:

- CAS updates oldv on failure, evaluates oldv once, and requires it to
  be an lvalue, as on Linux.
- Every macro fails to compile if the operand is not 4 or 8 bytes as
  the name says, on both platforms, so a width mismatch is caught on
  Linux before it reaches Solaris. All existing call sites pass.
- Results are cast to the operand's type with __typeof__((mem) + 0).
  Studio cc's __typeof__ keeps const even through (mem) + 0, and a
  statement expression with a const value can't be assigned from, so
  the cast is applied outside the statement expression; otherwise a
  load through a const pointer fails to compile.

Loads and release stores need no barrier instruction. Solaris runs user
code under TSO, where every load is an acquire and every store a
release, and every seq_cst write these macros perform is a cas/swap
RMW, which is a full fence on TSO (gcc emits no membar around them for
the same reason). So ATOMIC_LOAD32/64, ATOMIC_LOAD32_ACQ and
ATOMIC_STORE32_REL are a volatile access plus a compiler barrier, and
an aligned 32/64-bit access is a single instruction. Studio cc honours
the "memory" clobber.

TSO is guaranteed: on SPARC the memory model is the privileged
PSTATE.MM, which Solaris sets to TSO for user code, and sun4v CPUs
implement no other model. gcc on Solaris assumes the same: its default
is -mmemory-model=tso, and on sundev1 it compiles acquire loads and
release stores to a plain lduw/st, against membar #LoadLoad|#LoadStore
and #LoadStore|#StoreStore with -mmemory-model=rmo.

The Solaris macros are statement expressions rather than static inline
functions because Studio cc leaves inline functions as calls below
-xO3, and RelWithDebInfo builds with -g -xO2. always_inline is ignored
there too. Statement expressions inline at every level with both
Studio cc and gcc.

T5 cost with Studio cc -g -xO2, ns/op:

                          before   after
  ATOMIC_LOAD32/64          36      0.7
  LOAD32_ACQ/STORE32_REL    18      0.7
  ADD/XCHANGE/CAS           36      36-39 (libc cas/swap)

verify_atomics_work now also checks:
- CAS success and failure, including the oldv write-back, for signed,
  volatile and 64-bit operands where the high half matters;
- return values and signedness of XCHANGE, ADD and the loads;
- that each macro argument is evaluated once;
- loads through a const pointer and from a const volatile word, which
  fail to compile on Studio if the result is const-qualified;
- a CAS counter that relies on the oldv write-back, an XCHANGE
  spinlock, and acquire/release message passing;
- store buffering: XCHANGE then ATOMIC_LOAD of the other word must not
  let both threads miss each other's write. Replacing the XCHANGEs
  with plain stores makes this fail on both x86 and SPARC.
The handoff waits spin briefly and then yield, so the test still finishes
in about a second when threads outnumber cpus instead of taking minutes.
It uses an aborting CHECK instead of assert, which NDEBUG compiled
out, and rand_r instead of the non-thread-safe rand.

Tested: verify_atomics_work passes with gcc and clang on Linux, and on
Solaris 11.4 SPARC with Studio 12.6 (-g, -g -xO2, -xO3, -errwarn=%all)
and gcc (-O0, -O2, -Wall -Werror). Against the old header the test
fails on Solaris at the oldv write-back check. Full Linux builds with
gcc and clang compile cleanly.

Signed-off-by: Akshat Sikarwar <asikarwar1@bloomberg.net>
@akshatsikarwar

Copy link
Copy Markdown
Contributor Author

/plugin-branch atomic

@roborivers roborivers left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cbuild submission: Error ⚠.
RoboMark testing: submission_error.
Regression testing: Success ✓.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants